﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Threading;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

using BLL;
using DTO;
using DevExpress.XtraEditors.Repository;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmGeneralList : DevExpress.XtraEditors.XtraForm
    {
        public frmGeneralList()
        {
            InitializeComponent();
            grdViewTongQuat.BestFitColumns();
            grdViewTongQuat.OptionsView.ColumnAutoWidth = false;
            grdViewTongQuat.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewTongQuat.HorzScrollVisibility = ScrollVisibility.Always;
            grdTongQuat.UseEmbeddedNavigator = true;
            IntialLoad();
        }

        private BLsupervisor sup = new BLsupervisor();
        DataTranferEntity trans = new DataTranferEntity();
        private int ListStatus;

        #region IntialInterface
        private void mItemSource_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemSource.CheckState == CheckState.Checked)
            {
                grdBaSource.Visible = true;
            }
            else
            {
                grdBaSource.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemMother_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemMother.CheckState == CheckState.Checked)
            {
                grdBaMother.Visible = true;
            }
            else
            {
                grdBaMother.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemAddress_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemAddress.CheckState == CheckState.Checked)
            {
                grdBaAdress.Visible = true;
            }
            else
            {
                grdBaAdress.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemEDD_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemEDD.CheckState == CheckState.Checked)
            {
                grdBaEDD.Visible = true;
            }
            else
            {
                grdBaEDD.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemChild_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemChild.CheckState == CheckState.Checked)
            {
                grdBaChild.Visible = true;
            }
            else
            {
                grdBaChild.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mbtnDetail_Click(object sender, EventArgs e)
        {
            mbtnDetail.ShowDropDown();
        }        
        #endregion

        private void IntialLoad()
        {
            ListStatus = 0;
            cmbCity.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbCity.DisplayMember = "RegionName";
            cmbCity.ValueMember = "Region_Code";
            cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 3).ToList();
            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 4).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
            cmbCollector.DataSource = SharedData.CollectorList;
            cmbCollector.DisplayMember = "Collector_Code";
            cmbCollector.ValueMember = "Collector_ID";
            cmbCurrentBrand.DataSource = SharedData.BrandList;
            cmbCurrentBrand.DisplayMember = "Brand_Code";
            cmbCurrentBrand.ValueMember = "Brand_ID";
            cmbPreviousBrand.DataSource = SharedData.BrandList;
            cmbPreviousBrand.DisplayMember = "Brand_Code";
            cmbPreviousBrand.ValueMember = "Brand_ID";
        }

        private void LoadScreen()
        {
            Application.Run(new frmProcess());
        }

        private void btnShowList_Click(object sender, EventArgs e)
        {
            LoadCustomerFunc func = new LoadCustomerFunc();
            try
            {
                Thread t = new Thread(new ThreadStart(LoadScreen));
                t.Start();
                if (cmbList.SelectedIndex == -1)
                {
                    t.Abort();
                    MessageBox.Show("Chưa chọn loại danh sách cần xem", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    ListStatus = (int)cmbList.SelectedValue;
                   // var query = func.LoadListCustomerByCallStatus(ListStatus);
                    var query = func.LoadListCustomerByStatus(ListStatus);
                    grdTongQuat.DataSource = query;
                }
                t.Abort();
            }
            catch
            {
                return;
            }
            
        }

        private void cmbList_Click(object sender, EventArgs e)
        {
            cmbList.DataSource = SharedData.StatusList.Where(a => a.Parent_Code == SharedData.StatusList.Where(b => b.Status_Code == "LCS").SingleOrDefault().Status_ID).ToList();
            cmbList.DisplayMember = "StatusName";
            cmbList.ValueMember = "Status_ID";
        }

        



        


    }
}